<div class="easyui-layout" data-options="fit:true">
    <!-- Begin of toolbar -->
    <div id="wu-toolbar-cm">
        <div class="wu-toolbar-button">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAddCm()" plain="true">添加</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEditCm()" plain="true">修改</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeCm()" plain="true">删除</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="reloadCm()" plain="true">刷新</a>
        </div>
    </div>
    <!-- End of toolbar -->
    <table id="wu-datagrid-cm" class="easyui-datagrid" toolbar="#wu-toolbar-cm"></table>
</div>
<!-- Begin of easyui-dialog -->
<div id="wu-dialog-cm" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
    <form id="wu-form-cm" method="post">
        <table>
            <tr>
                <td width="60" align="right">栏目名称:</td>
                <td>
                    <input type="text" name="lm" class="wu-text" required="required"/>
                </td>
            </tr>
            <tr>
                <td align="right">位置:</td>
                <td>
                    <!-- <input type="text" name="sequenceNO" class="wu-text" /> -->
                    <select id="cm-select-weizhi" class="easyui-combobox" style="width:200px;" name="sequenceNO" required="required"></select>
                </td>
            </tr>
            <tr>
                <td align="right">排序:</td>
                <td>
                    <input type="text" name="id_desc" class="wu-text" required="required"/>
                </td>
            </tr>
            <input type="text" name="id" style="display:none;">
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
    /**
     * Name 添加记录
     */
    function addLm() {
        $('#wu-form-cm').form('submit', {
            url: $.webConfig.apiUrl + 'lm/addlm',
            success: function (data) {
                var data = JSON.parse(data);
                if (data['return_code']) {
                    $.messager.alert('信息提示', '提交成功！', 'info');
                    $('#wu-dialog-cm').dialog('close');
                    $('#wu-datagrid-cm').datagrid('reload');
                } else {
                    $.messager.alert('信息提示', '提交失败！', 'info');
                }
            }
        });
    }

    /**
     * Name 修改记录
     */
    function edit1() {
        $('#wu-form-cm').form('submit', {
            url: $.webConfig.apiUrl + 'lm/uplm',
            success: function (data) {
                var data = JSON.parse(data);
                if (data['return_code'] == 1) {
                    $.messager.alert('信息提示', '提交成功！', 'info');
                    $('#wu-dialog-cm').dialog('close');
                    $('#wu-datagrid-cm').datagrid('reload');
                } else {
                    $.messager.alert('信息提示', '提交失败！', 'info');
                }
            }
        });
    }

    /**
     * Name 删除记录
     */
    function removeCm() {
        var items = $('#wu-datagrid-cm').datagrid('getSelections');
        if (items.length==0) {
            $.messager.alert('信息提示', '你没选中哦...', 'info');
            return;
        }

        $.messager.confirm('信息提示', '确定要删除该记录？', function (result) {
            if (result) {
                var items = $('#wu-datagrid-cm').datagrid('getSelections');
                var ids = [];
                $(items).each(function () {
                    ids.push(this.id);
                });
                
                //alert(ids);return;
                $.ajax({
                    type: 'post',
                    url: $.webConfig.apiUrl + 'lm/dellm',
                    data: {
                        id: ids[0]
                    },
                    success: function (data) {
                        var data = JSON.parse(data);
                        if (data['return_code'] == 1) {
                            $.messager.alert('信息提示', '删除成功！', 'info');
                            $('#wu-datagrid-cm').datagrid('reload');
                        } else {
                            $.messager.alert('信息提示', '删除失败！', 'info');
                        }
                    }
                });
            }
        });
    }

    /**
     * Name 打开添加窗口
     */
    function openAddCm() {
        $('#wu-form-cm').form('clear');
        $('#wu-dialog-cm').dialog({
            closed: false,
            modal: true,
            title: "添加信息",
            cache: false,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: addLm
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog-cm').dialog('close');
                }
            }]
        });

        $('#cm-select-weizhi').combobox('setValue', '5');
    }

    /**
     * Name 打开修改窗口
     */
    function openEditCm() {
        $('#wu-form-cm').form('clear');
        var item = $('#wu-datagrid-cm').datagrid('getSelected');

        if (!item) {
            $.messager.alert('信息提示', '你没选中哦...', 'info');
            return;
        }

        $('#wu-form-cm').form('load', item);

        $('#wu-dialog-cm').dialog({
            closed: false,
            modal: true,
            title: "修改信息",
            cache: false,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: edit1
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog-cm').dialog('close');
                }
            }]
        });
        $('#cm-select-weizhi').combobox('setValue', item['sequenceNO']);
    }

    /**
     * Name 分页过滤器
     */
    function pagerFilter(data) {
        var pageData = {
            total: 18,
            rows: []
        };

        if (data['return_code'] == 1) {
            if (data['total']) {
                pageData['total'] = data['total'];
            }
            pageData['rows'] = data['return_body'];
        }

        return pageData;
    }


    var column_obj = {
        5: {
            id: 5,
            title: '不推荐到首页'
        },
        1: {
            id: 1,
            title: '轮播图旁边推荐'
        },
        2: {
            id: 2,
            title: '轮播图下面推荐'
        },
        3: {
            id: 3,
            title: '内容二部分推荐'
        },
        4: {
            id: 4,
            title: '内容三部分推荐'
        }
    }


    var column_arr = [{
            id: 5,
            title: '不推荐到首页'
        }, {
            id: 1,
            title: '轮播图旁边推荐'
        },
        {
            id: 2,
            title: '轮播图下面推荐'
        },
        {
            id: 3,
            title: '内容二部分推荐'
        },
        {
            id: 4,
            title: '内容三部分推荐'
        }
    ];


    $('#cm-select-weizhi').combobox({
        valueField: 'id',
        textField: 'title',
        required: true,
        data: column_arr,
        editable:false
    });
    /**
     * Name 载入数据
     */
    $('#wu-datagrid-cm').datagrid({
        url: $.webConfig.apiUrl + 'lm/getlm',
        loadFilter: pagerFilter,
        rownumbers: true,
        singleSelect: true,
        pageSize: 20,
        pagination: false,
        multiSort: true,
        fitColumns: true,
        fit: true,
        columns: [
            [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'id',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'lm',
                    title: '栏目名称',
                    width: 180,
                    sortable: true
                },
                // {
                //     field: 'fontcolor',
                //     title: '字体颜色',
                //     width: 100
                // },
                {
                    field: 'id_desc',
                    title: '排序',
                    width: 100
                },
                // {
                //     field: 'lm_path',
                //     title: '栏目路径',
                //     width: 100
                // },
                // {
                //     field: 'HtmlTitleZS',
                //     title: '文章总数',
                //     width: 100
                // },
                // {
                //     field: 'YueDu',
                //     title: '阅读数',
                //     width: 100
                // }, 
                {
                    field: 'sequenceNO',
                    title: '位置',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (column_obj[value]) {
                            return column_obj[value]['title'];
                        } else {
                            return value;
                        }

                    }
                }
            ]
        ]
    });

    // 刷新页面
    function reloadCm() {
        $('#wu-datagrid-cm').datagrid('reload');
    }
</script>